<?php

namespace StudyBuddy\Modules\Observers;

/**
 * This filter watches
 * the IP address of request
 * and can throw exception if 
 * request came from banned IP
 *
 */
class IpFilter extends \StudyBuddy\Observer {

    public function main() {
        d('get some event');
        switch ($this->eventName) {
            case 'onBeforeNewQuestion':
            case 'onBeforeNewAnswer':
            case 'onBeforeEdit':
                $this->checkIP();
                break;
        }
    }

    protected function checkIP() {
        $ip = \StudyBuddy\Request::getIP();
        d('checking IP: ' . $ip);
        $res = $this->oRegistry->Mongo->BANNED_IP->findOne(array('_id' => $ip));
        if (!empty($res)) {
            throw new \StudyBuddy\FilterException('Unable to add new content at this time');
        }
    }

}
